package com.zz.ecommerce.baseManage.baseDict;

import com.zz.ecommerce.baseManage.baseDict.bean.BaseDictBean;
import com.zz.ecommerce.baseManage.baseDict.bean.TDictManager;
import com.zz.ecommerce.baseManage.baseDict.service.BaseDictService;
import com.zz.ecommerce.common.CommonDao;
import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 基础字典管理
 */
@Controller
@RequestMapping("/baseDict")
public class TDictManagerController {
    @Autowired
    CommonDao commonDao;
    @Autowired
    BaseDictService baseDictService;

    /**
     * 基础字典管理界面
     *
     * @return
     */
    @RequestMapping(value = "TDictManagerManager", method = RequestMethod.GET)
    public String TDictManagerManager() {
        return "baseManager/baseDict/tdictmanager_manager";
    }

    /**
     * 查询基础字典列表
     *
     * @param gridRequest
     * @param tDictManager
     * @return
     */
    @RequestMapping(value = "qryTDictManagerPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult<TDictManager> qryTDictManagerPage(GridRequest gridRequest, TDictManager tDictManager) {
        List<TDictManager> list = commonDao.pageWhere("T_DICT_MANAGER", tDictManager, gridRequest.getStart(), gridRequest.getEnd(), "", "");
        long count = commonDao.countWhere("T_DICT_MANAGER", tDictManager, "");
        return new GridResult<TDictManager>(list, gridRequest, count);
    }

    /**
     * 保存基础字典详情
     *
     * @param session
     * @param tDictManager
     * @return
     */
    @RequestMapping(value = "saveTDictManager", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String saveTDictManager(HttpSession session, TDictManager tDictManager) {
        return "success";
    }

    /**
     * 获取基础字典详情
     *
     * @param tDictManager
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "getTDictManagerDetail", method = {RequestMethod.GET, RequestMethod.POST})
    public String getTDictManagerDetail(TDictManager tDictManager, ModelMap modelMap) {
        TDictManager tDictManagerDetail = (TDictManager) commonDao.qryOneCommon("T_DICT_MANAGER", tDictManager, "");
        modelMap.put("tDictManager", tDictManagerDetail);
        return "baseManager/baseDict/tdictmanager_detail";
    }

    /**
     * 查询基础字典列表
     *
     * @param gridRequest
     * @param tableName
     * @return
     */
    @RequestMapping(value = "qryDictContentPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult<BaseDictBean> qryDictContentPage(GridRequest gridRequest, String tableName, BaseDictBean dictBean) {
        TDictManager qryManager = new TDictManager();
        qryManager.setTableName(tableName);
        TDictManager dictManager = (TDictManager) commonDao.qryOneCommon("T_DICT_MANAGER", qryManager, "");
        List<BaseDictBean> list = baseDictService.pageWhere(dictManager, dictBean, gridRequest.getStart(), gridRequest.getEnd(), "");
        long count = baseDictService.countWhere(dictManager, dictBean);
        return new GridResult<BaseDictBean>(list, gridRequest, count);
    }

    /**
     * 查询基础字典列表
     *
     * @param tableName
     * @return
     */
    @RequestMapping(value = "getDictContentDetail", method = {RequestMethod.GET, RequestMethod.POST})
    public String qryDictContentPage(String tableName, String indx, ModelMap modelMap, String modify) {
        TDictManager qryManager = new TDictManager();
        qryManager.setTableName(tableName);
        TDictManager dictManager = (TDictManager) commonDao.qryOneCommon("T_DICT_MANAGER", qryManager, "");
        BaseDictBean dictBean = baseDictService.qryOne(dictManager, indx, "");
        modelMap.put("baseDict", dictBean);
        modelMap.put("dictManager", dictManager);
        modelMap.put("modify", "false".equals(modify));
        return "baseManager/baseDict/basedict_detail";
    }

    /**
     * 查询基础字典列表
     *
     * @param tableName
     * @return
     */
    @RequestMapping(value = "saveDictContentDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String saveDictContentDetail(String tableName,BaseDictBean dictBean ) {
        if (StringUtils.isNotEmpty(tableName) && null!=dictBean) {
            try {
                TDictManager qryManager = new TDictManager();
                qryManager.setTableName(tableName);
                TDictManager dictManager = (TDictManager) commonDao.qryOneCommon("T_DICT_MANAGER", qryManager, "");
                return baseDictService.saveDict(dictManager, dictBean);
            } catch (Exception e) {
                return "删除失败：" + e.getMessage();
            }
        }
        return "success";
    }

    /**
     * 删除基础字典内容
     *
     * @param tableName
     * @return
     */
    @RequestMapping(value = "deleteDictContentDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String deleteDictContentDetail(String tableName, String indxs) {
        if (StringUtils.isNotEmpty(tableName) && StringUtils.isNotEmpty(indxs)) {
            try {
                TDictManager qryManager = new TDictManager();
                qryManager.setTableName(tableName);
                TDictManager dictManager = (TDictManager) commonDao.qryOneCommon("T_DICT_MANAGER", qryManager, "");
                return baseDictService.deleteDict(dictManager, indxs);
            } catch (Exception e) {
                return "删除失败：" + e.getMessage();
            }
        }
        return "success";

    }
}
